﻿QUnit.test("AdviserByCurrentSession", function (assert) {
    
    advise = new AdviserByCurrentSession();
    isTime = advise.isTimeToDisplayAlert(17.8, 14000, 14000);
    assert.ok(isTime == false, "advise.isTimeToDisplayAlert(17.8, 14000, 14000)");
    return;
    isTime = advise.isTimeToDisplayAlert(17.8, 17999, 17999);
    assert.ok(isTime == false, "isTimeToDisplayAlert(17.8, 17999, 17999)");

    isTime = advise.isTimeToDisplayAlert(17.8, 18000, 18000);
    assert.ok(isTime == true, "isTimeToDisplayAlert(17.8, 18001, 18001)");

    advise.onAction(21000, enumAction.MAINTENANCE);
    avgResponse = advise.getResponseDelay();
    assert.ok(avgResponse == 3000, "response1 " + avgResponse);
    
    assert.ok(advise.isTimeToDisplayAlert(10.0, 12999, 34000) == false, "advise.isTimeToDisplayAlert(10.0, 12999, 34000)");
    assert.ok(advise.isTimeToDisplayAlert(10.0, 13000, 34000) == true, "advise.isTimeToDisplayAlert(10.0, 13000, 34000)");

    advise.onAction(36000, enumAction.MAINTENANCE);
    avgResponse = advise.getResponseDelay();
    assert.ok(avgResponse == 2500, "response2 " + avgResponse);

    // test ignoring from delay larger than 8 sec.
    assert.ok(advise.isTimeToDisplayAlert(10.0, 16000 - avgResponse, 34000) == true, "advise.isTimeToDisplayAlert(10.0, 13000, 34000)");
    advise.onAction(96000, enumAction.MALFUNCTION_OCCUR);
    avgResponse = advise.getResponseDelay();
    assert.ok(avgResponse == 2500, "response2 " + avgResponse);

    assert.ok(advise.isTimeToDisplayAlert(10.0, 16000 - avgResponse, 20000) == true, "advise.isTimeToDisplayAlert(10.0, 13000, 34000)");
    freezeDuration = 4000;
    advise.onSpaceshipFreeze(21000, freezeDuration);
    advise.onAction(28000, enumAction.MALFUNCTION_OCCUR);
    avgResponse = advise.getResponseDelay();
    assert.ok(avgResponse == 3000, "response3 " + avgResponse);
});

QUnit.test("LastTimeSegmentPerformanceCalculator", function (assert) {
    
    calc = new LastTimeSegmentPerformanceCalculator(20000, 30, 10);
    calc.onAction(200, enumAction.SHUTDOWN);
    assert.ok(calc.getNetPerformance().netScore == 18);
    for (var i = 100; i <= 10000; i += 100) {
        calc.onAction(i, enumAction.SHUTDOWN);
        console.log(i + ", " + calc.getNetPerformance().netScore);
    }
    assert.ok(calc.getNetPerformance().netScore == 300);


    calc = new LastTimeSegmentPerformanceCalculator(20000, 30, 10);
    for (var i = 0; i <= 20000; i += 250) {
        calc.onAction(i, enumAction.SHUTDOWN);
        console.log(i + ", " + calc.buckets.length + "," + calc.numOfBuckets + "," + calc.getNetPerformance(i).netScore);
    }
    assert.ok(calc.getNetPerformance().netScore == 120);

    calc = new LastTimeSegmentPerformanceCalculator(20000, 30, 10);
    for (var i = 0; i <= 20000; i += 250) {
        if (i == 10000) {
            calc.onSpaceshipFreeze(i,4000);
            i += 4000;
        }
        calc.onAction(i, enumAction.SHUTDOWN);
        console.log(i + ", " + calc.buckets.length + "," + calc.numOfBuckets + "," + calc.getNetPerformance(i).netScore);
    }
    assert.ok(calc.getNetPerformance().netScore == 120);

    calc = new LastTimeSegmentPerformanceCalculator(20000, 30, 10);
    endTime = 28000;
    for (var i = 0; i <= endTime; i += 1000) {
        calc.onAction(i, enumAction.SHUTDOWN);
        console.log(i + ", " + calc.buckets.length + "," + calc.numOfBuckets + "," + calc.getNetPerformance(i).netScore);
        if (i == 10000 || i == 20000) {
            calc.onSpaceshipFreeze(i,4000);
            i += 4000;
            //calc.onAction(i, enumAction.SHUTDOWN);
            console.log(i + ", " + calc.buckets.length + "," + calc.numOfBuckets + "," + calc.getNetPerformance(i).netScore);
        }
        
    }
    console.log("netTime\t", calc.getNetPerformance(endTime).netTime);
    assert.equal(30 , calc.getNetPerformance().netScore, "");
    //assert.ok(calc.getNetPerformance().netScore == 30);
    
});

QUnit.test("testSound", function (assert) {
    var sound = new Audio("../sounds/mntAlert.mp3");
    sound.play();
    setTimeout(function () { sound.pause(); }, 3000);

    setTimeout(function () { sound.load(); sound.play(); }, 1000);
    setTimeout(function () { sound.stop(); }, 1000);
    setTimeout(function () { sound.load(); sound.play(); }, 1000);
    //sound.stop();
});
